﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.ApproveFlow.Buz.Interfaces;
using Microhard.ApproveFlow.Buz.DataAccess;
using Microhard.Core.DBUtility;
using Microhard.ApproveFlow.Buz.DTO;
using Microhard.Core;
using Microhard.ApproveFlow.Buz.Msg;
using Microhard.ApproveFlow.Buz.Models;

namespace Microhard.ApproveFlow.Buz.Impls
{
    public class FlowChartImpl : IFlowChart
    {
        public bool IsFlowChartNameDuplicate(Models.FlowChartModel model)
        {
            MicroHardApproveFlowEntities db = new MicroHardApproveFlowEntities();
            var entity = db.t_FlowChart.SingleOrDefault(s => s.FlowChartName == model.FlowChartName);
            if (entity != null)
            {
                if (entity.FlowChartId == model.FlowChartId)
                    return false;
            }
            else
            {
                return false;
            }
            return true;
        }

        public Core.DBUtility.ExecutedResult Add(Models.FlowChartModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicroHardApproveFlowEntities db = new MicroHardApproveFlowEntities();

            var op = model.ToEntity();
            op.CreateDate = DateTime.Now;
            op.LastUpdateDate = op.CreateDate;

            try
            {
                db.t_FlowChart.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, FlowChartModelMsg.AddFailed);
                Log.Error("IFlowChart.Add", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Update(Models.FlowChartModel model)
        {
            ExecutedResult result = new ExecutedResult();
            MicroHardApproveFlowEntities db = new MicroHardApproveFlowEntities();
            var entity = db.t_FlowChart.SingleOrDefault(l => l.FlowChartId == model.FlowChartId);
            if (entity != null)
            {
                entity.FlowChartName = model.FlowChartName;
                entity.TypeId = model.TypeId;
                entity.LastUpdateDate = DateTime.Now;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, FlowChartModelMsg.UpdateFailed);
                Log.Error("IFlowChart.Update", ex);
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            MicroHardApproveFlowEntities db = new MicroHardApproveFlowEntities();
            var entity = db.t_FlowChart.Where(l => ids.Contains(l.FlowChartId));
            foreach (var e in entity)
            {
                if (e.t_WorkflowDyncStatus.Count > 0)
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, string.Format(FlowChartModelMsg.IsUsing, e.FlowChartName));
                }
                else
                {
                    db.t_FlowChart.DeleteObject(e);
                }
            }
            if (result.ErrorMsg.Count == 0)
            {
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    result.ErrorMsg.Add(DBOperationFailedStatus.DeleteFailed, FlowChartModelMsg.DeleteFailed);
                    Log.Error("IFlowChart.Delete", ex);
                }
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Delete(Models.FlowChartModel model)
        {
            throw new NotImplementedException();
        }

        public Models.FlowChartModel GetModelByID(Guid id)
        {
            throw new NotImplementedException();
        }

        public Models.FlowChartModel GetModelByModel(Models.FlowChartModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.FlowChartModel> Query(Models.FlowChartModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            throw new NotImplementedException();
        }

        public List<Models.FlowChartModel> Query(Models.FlowChartModel Model)
        {
            List<FlowChartModel> result = new List<FlowChartModel>();
            MicroHardApproveFlowEntities db = new MicroHardApproveFlowEntities();
            var entities = db.t_FlowChart.OrderByDescending(s => s.FlowChartName).ToList();
            if (entities.Count > 0)
            {
                result = entities.Select(p => p.ToModel()).ToList();
            }
            return result;
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }
    }
}
